// packageD/pages/chart2/chart2.js
import * as echarts from '../../../ec-canvas/echarts.min.js';
import { getUserInfo } from '../../../utils/userInfo';
import handleHistory from '../utils/handleHistory';
const app = getApp();

function initChart(canvas, width, height, dpr) {
    const { _id } = getUserInfo();
    // 当前时间 currentDate
    let currentDate = new Date();
    // 此前七天 agoTime
    let agoTime = currentDate.setDate(currentDate.getDate() - 7);
    // let agoTime = currentDate.setDate(currentDate.getDate() - 30);
    // 获取七天内发布的错题的信息
    wx.cloud.callFunction({
        // 云函数名称
        name: 'reqQuestionAPI',
        // 传给云函数的参数
        data: {
            scene: 'GET_AGO_TIME_QUESTION',
            uid: _id,
            agoTime
        },
    })
        .then(res => {
            // console.log(res.result);
            // 归类，转换为一天中，多少数量错题的形式
            console.log(handleHistory(res.result.data));
            const lineData = handleHistory(res.result.data);

            const chart = echarts.init(canvas, null, {
                width: width,
                height: height,
                devicePixelRatio: dpr // new
            });
            canvas.setChart(chart);

            var option = {
                title: {
                    text: '最近七天',
                    left: 'center'
                },
                // legend: {
                //     data: ['A'],
                //     top: 50,
                //     left: 'center',
                //     backgroundColor: 'red',
                //     z: 100
                // },
                grid: {
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'axis'
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: lineData.map(item => item.creatDate),
                    // show: false
                },
                yAxis: {
                    x: 'center',
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                    // show: false
                },
                series: [{
                    // name: 'A',
                    type: 'line',
                    smooth: true,
                    data: lineData.map(item => item.list.length),
                }]
            };

            chart.setOption(option);
            return chart;
        })
        .catch(console.error)

};

function initChart2(canvas, width, height, dpr) {
    const { _id } = getUserInfo();
    // 当前时间 currentDate
    let currentDate = new Date();
    // 此前30天 agoTime
    // let agoTime = currentDate.setDate(currentDate.getDate() - 7);
    let agoTime = currentDate.setDate(currentDate.getDate() - 30);
    // 获取30天内发布的错题的信息
    wx.cloud.callFunction({
        // 云函数名称
        name: 'reqQuestionAPI',
        // 传给云函数的参数
        data: {
            scene: 'GET_AGO_TIME_QUESTION',
            uid: _id,
            agoTime
        },
    })
        .then(res => {
            // console.log(res.result);
            // 归类，转换为一天中，多少数量错题的形式
            console.log(handleHistory(res.result.data));
            const lineData = handleHistory(res.result.data);

            const chart = echarts.init(canvas, null, {
                width: width,
                height: height,
                devicePixelRatio: dpr // new
            });
            canvas.setChart(chart);

            var option = {
                title: {
                    text: '最近一个月',
                    left: 'center'
                },
                grid: {
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'axis'
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: lineData.map(item => item.creatDate),
                    // show: false
                },
                yAxis: {
                    x: 'center',
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                    // show: false
                },
                series: [{
                    // name: 'A',
                    type: 'line',
                    smooth: true,
                    data: lineData.map(item => item.list.length),
                }]
            };

            chart.setOption(option);
            return chart;
        })
        .catch(console.error)

}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ec: {
            onInit: initChart
        },
        ec2: {
            onInit: initChart2
        },
        showPage: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        setTimeout(() => {
            that.setData({
                showPage: true,
            });
        }, 2000);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
            title: '近期录入错题情况'
          })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})